/*
	1 管理员资质确认
	2 获取接口内容 -- 铺陈页面--铺陈id
	3 修改内容
	4 小程序：
		播放视频函数
*/

//启用layui
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//轮播索引
var itemIndex = 0;
//总轮播项目
var totleBannerNum = 0;
//轮播数据
var bannerData = [];

$(function(){
	layui.use(['carousel', 'layer', 'form'], function(){
		var carousel = layui.carousel;
		var layer = layui.layer;
		var form = layui.form;
	 	
	 	// ajax data
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		}
		//播放视频
		function playVideo(videoSrc, videoType, mainPicSrc){ 
			//播放内容构建
			var videoBox = '<video id="videoContent" class="video-js" controls preload="auto" width="1600" height="840" poster="'+mainPicSrc+'">'+ 
					'<source src="'+videoSrc+'" type="video/'+videoType+'">'
				+'</video>';
			//弹出播放器		
			layer.open({
				type: 1,
				skin: 'layui-layer-rim',
				area: ['80%', '80%'],
				title:'七牛云视频--' + videoSrc,
				content: videoBox,
				success: function(){
					//执行播放
					var videoPlayer = videojs('videoContent');
					videoPlayer.ready(function(){
						var player = this;
						player.play();
					})
				}
			});	
		};
		//播放视频
		form.on('submit(play-video)', function(data){
			//console.log(data);
			playVideo(data.field.banner_video,'mp4', data.field.banner_img);
			return false;
		})
		//轮播图
		function bannerCarousel(){
			var options = {
			    elem: '#topVideoBanner',
			    width: '100%' ,
			    arrow: 'always',
			    autoplay: false
			  }
			//启用轮播
			carousel.render(options);  
			//监听轮播切换事件
			carousel.on('change(topVideoBanner)', function(obj){ 
				//console.log(obj.index); 
				//console.log(obj.prevIndex);
				//console.log(obj.item); 
				$('#bannerItemIndex').text(obj.index + 1);
				changeFormDataShow(obj.index);
				itemIndex = obj.index;
			}); 
		}

		// 获取轮播数据
		function getMainTopBanner(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, {'pg':'index', 'ele':'tpb'}, ajaxData)
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'GET',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						bannerData = responseData.data;
						totleBannerNum = responseData.data.length;
						initBannerToPage(responseData);
						//启动轮播
						bannerCarousel()
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		}
		//铺陈轮播数据
		function initBannerToPage(responseData){
			var data = responseData.data;
			var carousel = $('#topVideoBanner .carousel-item');
			for (var i = 0; i < data.length; i++) {
				var item = DOMHTMLSTRINGS.top_banner_item(data, i);
				carousel.append(item);
			};
			changeFormDataShow();
		}
		//表单数据切换
		function changeFormDataShow(index){   //index 为当前轮播项
			var _index = 0;
			index ? _index = index : _index = 0;
			var form_data = {
				text_cn1: bannerData[_index].cn[0],
				text_cn2: bannerData[_index].cn[1],
				text_cn3: bannerData[_index].cn[2],
				text_tw1: bannerData[_index].tw[0],
				text_tw2: bannerData[_index].tw[1],
				text_tw3: bannerData[_index].tw[2],
				text_en1: bannerData[_index].en[0],
				text_en2: bannerData[_index].en[1],
				text_en3: bannerData[_index].en[2],
				banner_img: bannerData[_index].img,
				banner_video: bannerData[_index].video,
				link: bannerData[_index].item_link
			};
			jQuery('input[name="has_video"][value="'+bannerData[_index].has_video+'"]').prop('checked', 'checked').siblings().prop('checked', '');
			form.val('banner-item', form_data);
			form.render('radio');
		}
		//保存修改
		form.on('submit(modify)', function(data){
			var field = data.field
			var form_data ={
				cn: field.text_cn1 + '|' + field.text_cn2 + '|' + field.text_cn3,
				tw: field.text_tw1 + '|' + field.text_tw2 + '|' + field.text_tw3, 
				en: field.text_en1 + '|' + field.text_en2 + '|' + field.text_en3,
				img: field.banner_img,
				video: field.banner_video,
				index: String(itemIndex),  //次序索引
				hasVideo: field.has_video,
				link: field.link
			};
			//console.log(form_data)
			layer.confirm(MESSAGES.confirm_modify, {title:'提示'}, function(index){
				console.log(form_data)
				modifyBannerItem(form_data);
				layer.close(index);
			});
			return false;
		});
		//修改轮播项
		function modifyBannerItem(bannerData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({'pg':'index', 'ele':'tpb'}, bannerData, ajaxData);
			//console.log(_ajaxData);
			//return false;
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type: 'POST',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						console.log(responseData)
						//location.reload();
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time:2000, end: function(){
						location.href = './500.html';
					}})
				}
			})
		}
		//新增项目
		function newAddItem(ajaxData){
			//新增项目，给予预设值，跳转项目  --然后可进行修改内容
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			//新增内容的索引
			var _index = totleBannerNum;
			var bannerData = {
				cn: '轮播text|轮播text|轮播text',
				tw: '轮播text|轮播text|轮播text', 
				en: '轮播text|轮播text|轮播text',
				img: 'http://exp.com/img',
				video: '',
				has_video:'false',
				index: String(_index)  //次序索引
			}
			var _ajaxData = $.extend({'pg':'index', 'ele':'tpb'}, bannerData, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'PUT',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						//console.log(responseData);
						location.reload();
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},	
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}})
				}
			})
		};
		//新增
		$('#newAdd').on('click', function(){
			layer.confirm(MESSAGES.confirm_add, {title:'提示'}, function(index){
				newAddItem(ajaxData);
				layer.close(index);
			});
		});
		//删除当前轮播项
		form.on('submit(delete)', function(data){
			//console.log(itemIndex);
			if(itemIndex === 0){
				layer.msg('轮播首项不能被删除！', {time: 2000});
				return false;
			}
			var itme_index ={
				index: String(itemIndex)  //次序索引
			};
			layer.confirm(MESSAGES.confirm_delete, {title:'提示'}, function(index){
				deleteBannerItem(itme_index)
				layer.close(index);
			});
			return false;
		})
		//删除当前项算法
		function deleteBannerItem(itme_index){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({'pg':'index', 'ele':'tpb'}, itme_index, ajaxData)
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'DELETE',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						console.log(responseData)
						layer.msg(MESSAGES.delete_success, {time: 2000, end: function(){
							//location.reload();
						}})
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}})
				} 
			})
		}

		//顶图1 
		function getTopImg1Info(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, {'pg':'index', 'ele':'tpi1'}, ajaxData)

			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'GET',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						var d = responseData.data[0]
						//铺陈数据
						form.val('topImg1Form', {
							indexTopImg1: d.img,
							topImgLink: d.link,
							topImgtextLine1Cn: d.cn[0],
							topImgtextLine2Cn: d.cn[1],
							topImgtextLine3Cn: d.cn[2],
							topImgtextLine1Tw: d.tw[0],
							topImgtextLine2Tw: d.tw[1],
							topImgtextLine3Tw: d.tw[2],
							topImgtextLine1En: d.en[0],
							topImgtextLine2En: d.en[1],
							topImgtextLine3En: d.en[2]
						});

					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		};
		function modifyTopImg1Info(data){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend(data, {'pg':'index', 'ele':'tpi1'}, ajaxData)
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'POST',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						location.reload();
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		};
		//确认修改
		form.on('submit(topImg1FormModify)', function(data){
			console.log(data);
			var f = data.field;
			layer.confirm(MESSAGES.confirm_modify, {title:'提示'}, function(index){
				modifyTopImg1Info({
					img: f.indexTopImg1,
					link: f.topImgLink,
					cn: f.topImgtextLine1Cn + "|" + f.topImgtextLine2Cn + "|" + f.topImgtextLine3Cn,
					tw: f.topImgtextLine1Tw + "|" + f.topImgtextLine2Tw + "|" + f.topImgtextLine3Tw,
					en: f.topImgtextLine1En + "|" + f.topImgtextLine2En + "|" + f.topImgtextLine3En,
				})
				layer.close(index);
			});
			return;
		})

		//顶图2
		function getTopImg2Info(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, {'pg':'index', 'ele':'tpi2'}, ajaxData)

			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'GET',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						var d = responseData.data[0]
						//铺陈数据
						form.val('topImg2Form', {
							indexTopImg2: d.img,
							topImg2Link: d.link,
							topImg2textLine1Cn: d.cn[0],
							topImg2textLine2Cn: d.cn[1],
							topImg2textLine3Cn: d.cn[2],
							topImg2textLine1Tw: d.tw[0],
							topImg2textLine2Tw: d.tw[1],
							topImg2textLine3Tw: d.tw[2],
							topImg2textLine1En: d.en[0],
							topImg2textLine2En: d.en[1],
							topImg2textLine3En: d.en[2]
						});
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		}
		function modifyTopImg2Info(data){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend(data, {'pg':'index', 'ele':'tpi2'}, ajaxData)
			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'POST',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						location.reload();
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		};
		//确认修改
		form.on('submit(topImg2FormModify)', function(data){
			//console.log(data);
			var f = data.field;
			layer.confirm(MESSAGES.confirm_modify, {title:'提示'}, function(index){
				modifyTopImg2Info({
					img: f.indexTopImg2,
					link: f.topImg2Link,
					cn: f.topImg2textLine1Cn + "|" + f.topImg2textLine2Cn + "|" + f.topImg2textLine3Cn,
					tw: f.topImg2textLine1Tw + "|" + f.topImg2textLine2Tw + "|" + f.topImg2textLine3Tw,
					en: f.topImg2textLine1En + "|" + f.topImg2textLine2En + "|" + f.topImg2textLine3En,
				})
				layer.close(index);
			});
			return;
		});

		/* 三联 区块 */
		function getSanlanInfo(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, {'pg':'index', 'ele':'sanlan'}, ajaxData)

			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'GET',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						var d = responseData.data[0];
						//铺陈数据
						form.val('sanlanImgForm', {
							sanlanImg1: d.img_l,
							sanlanImg2: d.img_rt,
							sanlanImg3: d.img_rb,
							sanlan1textline1Cn: d.text_l.line_1.cn,
							sanlan1textline1Tw: d.text_l.line_1.tw,
							sanlan1textline1En: d.text_l.line_1.en,
							sanlan1textline2Cn: d.text_l.line_2.cn,
							sanlan1textline2Tw: d.text_l.line_2.tw,
							sanlan1textline2En: d.text_l.line_2.en,
							sanlan1textline3Cn: d.text_l.line_3.cn,
							sanlan1textline3Tw: d.text_l.line_3.tw,
							sanlan1textline3En: d.text_l.line_3.en,
							sanlan1textRTline1Cn: d.text_rt.line_1.cn,
							sanlan1textRTline1Tw: d.text_rt.line_1.tw,
							sanlan1textRTline1En: d.text_rt.line_1.en,
							sanlan1textRTline2Cn: d.text_rt.line_2.cn,
							sanlan1textRTline2Tw: d.text_rt.line_2.tw,
							sanlan1textRTline2En: d.text_rt.line_2.en,
							sanlan1textRBline1Cn: d.text_rb.line_1.cn,
							sanlan1textRBline1Tw: d.text_rb.line_1.tw,
							sanlan1textRBline1En: d.text_rb.line_1.en,
							sanlan1textRBline2Cn: d.text_rb.line_2.cn,
							sanlan1textRBline2Tw: d.text_rb.line_2.tw,
							sanlan1textRBline2En: d.text_rb.line_2.en,
							link_l: d.link_l,
							link_rt: d.link_rt,
							link_rb: d.link_rb,
						});
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		}
		function modefySanlanInfo(data){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend(data, {'pg':'index', 'ele':'sanlan'}, ajaxData)

			$.ajax({
				url: BASE_SERVICEURL + URlS.page,
				type:'POST',
				data: _ajaxData,
				success: function(responseData){			
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						location.reload();
					}else{
						layer.msg(responseData.msg.msg, {time:2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex)
					layer.msg(MESSAGES.service_error, {time:2000, end:function(){
						window.location.href = './500.html';
					}})
				}
			});
		}
		form.on('submit(sanlanImgFormModify)', function(data){
			var f = data.field;
			layer.confirm(MESSAGES.confirm_modify, {title:'提示'}, function(index){
				modefySanlanInfo({
					img: f.sanlanImg1 + "|" + f.sanlanImg2 + "|" + f.sanlanImg3,
					cn: f.sanlan1textline1Cn + "|" + f.sanlan1textline2Cn + "|" + f.sanlan1textline3Cn + "|" + f.sanlan1textRTline1Cn + "|" + f.sanlan1textRTline2Cn + "|" + f.sanlan1textRBline1Cn + "|" + f.sanlan1textRBline2Cn,
					tw: f.sanlan1textline1Tw + "|" + f.sanlan1textline2Tw + "|" + f.sanlan1textline3Tw + "|" + f.sanlan1textRTline1Tw + "|" + f.sanlan1textRTline2Tw + "|" + f.sanlan1textRBline1Tw + "|" + f.sanlan1textRBline2Tw,
					en: f.sanlan1textline1En + "|" + f.sanlan1textline2En + "|" + f.sanlan1textline3En + "|" + f.sanlan1textRTline1En + "|" + f.sanlan1textRTline2En + "|" + f.sanlan1textRBline1En + "|" + f.sanlan1textRBline2En,
					link: f.link_l + "|" + f.link_rt + "|" + f.link_rb 
				})
				layer.close(index);
			});
			return;
		})







		getMainTopBanner(ajaxData)
		getTopImg1Info(ajaxData)
		getTopImg2Info(ajaxData)
		getSanlanInfo(ajaxData)
	})
})
































